package com.yy.jhl.ui.manager.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.LogisticsInfo;
import com.yy.jhl.ui.manager.contract.LogisticsContract;
import com.yy.jhl.ui.manager.presenter.LogisticsPresenter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by libingn on 2017/12/7.
 */

public class LogisticsManagerActivity extends BaseActivity<LogisticsPresenter> implements LogisticsContract.View {

    @BindView(R.id.order_name_text)
    TextView orderNameText;
    @BindView(R.id.order_code_text)
    TextView orderCodeText;
    @BindView(R.id.batch_text)
    TextView batchText;
    @BindView(R.id.gys_text)
    TextView gysText;
    @BindView(R.id.wuliu_icon)
    ImageView wuliuIcon;
    @BindView(R.id.wuliu_line)
    View wuliuLine;
    @BindView(R.id.wuliu_title_text)
    TextView wuliuTitleText;
    @BindView(R.id.wuliu_time_text)
    TextView wuliuTimeText;
    @BindView(R.id.wuliu_new_text)
    TextView wuliuNewText;
    @BindView(R.id.wuliu_parent)
    LinearLayout wuliuParent;
    @BindView(R.id.chuku_icon)
    ImageView chukuIcon;
    @BindView(R.id.chuku_line)
    View chukuLine;
    @BindView(R.id.chuku_title_text)
    TextView chukuTitleText;
    @BindView(R.id.chuku_content_text)
    TextView chukuContentText;
    @BindView(R.id.chuku_time_text)
    TextView chukuTimeText;
    @BindView(R.id.chuku_parent)
    LinearLayout chukuParent;
    @BindView(R.id.box_icon)
    ImageView boxIcon;
    @BindView(R.id.box_line)
    View boxLine;
    @BindView(R.id.box_title_text)
    TextView boxTitleText;
    @BindView(R.id.box_content_text)
    TextView boxContentText;
    @BindView(R.id.box_time_text)
    TextView boxTimeText;
    @BindView(R.id.box_parent)
    LinearLayout boxParent;
    @BindView(R.id.chuku_after_icon)
    ImageView chukuAfterIcon;
    @BindView(R.id.chuku_after_line)
    View chukuAfterLine;
    @BindView(R.id.chuku_after_title_text)
    TextView chukuAfterTitleText;
    @BindView(R.id.chuku_after_content_text)
    TextView chukuAfterContentText;
    @BindView(R.id.chuku_after_time_text)
    TextView chukuAfterTimeText;
    @BindView(R.id.chuku_after_parent)
    LinearLayout chukuAfterParent;
    @BindView(R.id.gys_parent)
    LinearLayout gysParent;
    @BindView(R.id.home_icon)
    ImageView homeIcon;
    @BindView(R.id.home_line)
    View homeLine;
    @BindView(R.id.home_title_text)
    TextView homeTitleText;
    @BindView(R.id.home_parent)
    LinearLayout homeParent;
    @BindView(R.id.abroad_icon)
    ImageView abroadIcon;
    @BindView(R.id.abroad_line)
    View abroadLine;
    @BindView(R.id.abroad_title_text)
    TextView abroadTitleText;
    @BindView(R.id.abroad_parent)
    LinearLayout abroadParent;
    @BindView(R.id.content_parent)
    LinearLayout contentParent;
    String batchId = "";
    @BindView(R.id.end_icon)
    ImageView endIcon;
    @BindView(R.id.end_title_text)
    TextView endTitleText;
    @BindView(R.id.chuku_new_text)
    TextView chukuNewText;
    @BindView(R.id.box_new_text)
    TextView boxNewText;
    @BindView(R.id.home_new_text)
    TextView homeNewText;
    @BindView(R.id.chuku_after_new_text)
    TextView chukuAfterNewText;
    @BindView(R.id.end_parent)
    LinearLayout endParent;
    @BindView(R.id.fengxiang_operation_icon)
    ImageView fengxiangOperationIcon;
    @BindView(R.id.chuku_operation_icon)
    ImageView chukuOperationIcon;
    //1 :供应商 2：采购商
    private int currentLoginType = 1;
    String progressStatus = "";//1 3 5 7 9 11 13
    String status = "";//审核状态1、默认2、通过3、拒绝4、留言
    LogisticsInfo logisticsInfo;

    public static void startAction(Context mContext, String batchId) {
        Intent intent = new Intent(mContext, LogisticsManagerActivity.class);
        intent.putExtra("batchId", batchId);
        mContext.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.act_logistics_manager;
    }

    @Override
    public void initView() {
        currentLoginType = SPUtils.getSharedIntData(mContext, AppConstant.SP_KEY_LOGIN_TYPE);
        contentParent.setVisibility(View.GONE);
        chukuParent.setClickable(false);
        boxParent.setClickable(false);
        chukuAfterParent.setClickable(false);
        homeParent.setClickable(false);
        abroadParent.setClickable(false);
        endParent.setClickable(false);
        batchId = getIntent().getStringExtra("batchId");
        showTitle("物流监管", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        if (currentLoginType == 1){
            gysParent.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onStart() {
        mPresenter.getProjectDetail(batchId);
        super.onStart();
    }

    @OnClick({R.id.wuliu_parent, R.id.chuku_parent, R.id.box_parent, R.id.chuku_after_parent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wuliu_parent:
                break;
            case R.id.chuku_parent:
                if (currentLoginType == 1) {
                    //供应商
                    OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                } else {
                    //采购商
                    if ("1".equals(status)) {
                        OutboundCheckActivity.startAction(mContext, logisticsInfo);
                    } else {
                        OutboundViewActivity.startAction(mContext, logisticsInfo, status);
                    }
                }
                break;
            case R.id.box_parent:
                //封箱信息
                SealBoxListActivity.startAction(mContext, batchId, logisticsInfo.getOrder_id(), status,progressStatus,logisticsInfo);
                break;
            case R.id.chuku_after_parent:
                //出库
                if (Integer.parseInt(progressStatus) > 7) {
                    OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, "2");
                } else {
                    if ("2".equals(status)) {
                        if (currentLoginType == 1) {
                            OutboundAfterActivity.startAction(mContext, logisticsInfo);
                        }
                    } else {
                        OutboundAfterCheckActivity.startAction(mContext, logisticsInfo, status);
                    }
                }
                break;
        }
    }

    @Override
    public void getProjectDetail(LogisticsInfo projectDetailInfo) {
        logisticsInfo = projectDetailInfo;
        contentParent.setVisibility(View.VISIBLE);
        orderNameText.setText(projectDetailInfo.getTitle());
        orderCodeText.setText(projectDetailInfo.getOrder_sn());
        batchText.setText(projectDetailInfo.getBatch_sn());
        gysText.setText(projectDetailInfo.getCompany_name());
        wuliuTimeText.setText(projectDetailInfo.getCreate_time());
        chukuTimeText.setText(projectDetailInfo.getCreate_time());
        progressStatus = projectDetailInfo.getShip_status();
        status = projectDetailInfo.getStatus();
//        try {
//            JSONArray shipList = new JSONArray(projectDetailInfo.getShip_list());
            boolean isShowHome = false;
            boolean isShowAbroad = false;
            String homeAddress = "";
            String abroadAddress = "";
            String shipList = projectDetailInfo.getShip_list();
            if (!StringUtil.isEmpty(shipList) && shipList.contains("9")){
                isShowHome = true;
                homeAddress = StringUtil.isEmpty(projectDetailInfo.getInland_agent_id()) ? "" : projectDetailInfo.getInland_agent_id();
            }
            if (!StringUtil.isEmpty(shipList) && shipList.contains("11")){
                isShowAbroad = true;
                abroadAddress = StringUtil.isEmpty(projectDetailInfo.getForeign_agent_id()) ? "" : projectDetailInfo.getForeign_agent_id();
            }
//            if (shipList != null && shipList.length() > 0) {
//                for (int i = 0; i < shipList.length(); i++) {
//                    JSONObject object = shipList.optJSONObject(i);
//                    if ("9".equals(object.optString("status"))) {
//                        homeAddress = object.optString("address");
//                        isShowHome = true;
//                    }
//                    if ("11".equals(object.optString("status"))) {
//                        isShowAbroad = true;
//                        abroadAddress = object.optString("address");
//                    }
//                }
//            }
            //是否有国内货代
            if (isShowHome) {
                homeParent.setVisibility(View.VISIBLE);
                final String finalHomeAddress = homeAddress;
                homeParent.setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        //国内货代
                        FreightForwardActivity.startAction(mContext, 1, batchId, finalHomeAddress,progressStatus,logisticsInfo.getShip_list());
                    }
                });
            } else {
                homeParent.setVisibility(View.GONE);
            }
            //是否有国外货代
            if (isShowAbroad) {
                abroadParent.setVisibility(View.VISIBLE);
                final String finalAbroadAddress = abroadAddress;
                abroadParent.setOnClickListener(new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        //国外货代
                        FreightForwardActivity.startAction(mContext, 2, batchId, finalAbroadAddress);
                    }
                });
            } else {
                abroadParent.setVisibility(View.GONE);
            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        switch (progressStatus) {
            case "1":
                //开始
                wuliuLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                chukuIcon.setImageResource(R.drawable.gray_round_bg);
                chukuLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                boxIcon.setImageResource(R.drawable.gray_round_bg);
                boxLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                chukuAfterIcon.setImageResource(R.drawable.gray_round_bg);
                chukuAfterLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                homeIcon.setImageResource(R.drawable.gray_round_bg);
                homeLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                abroadIcon.setImageResource(R.drawable.gray_round_bg);
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                boxContentText.setVisibility(View.GONE);
                fengxiangOperationIcon.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                break;
            case "3":
                //出库准备
                chukuParent.setClickable(true);
                boxParent.setClickable(false);
                chukuAfterParent.setClickable(false);
                homeParent.setClickable(false);
                abroadParent.setClickable(false);
                endParent.setClickable(false);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                boxIcon.setImageResource(R.drawable.gray_round_bg);
                boxLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                chukuAfterIcon.setImageResource(R.drawable.gray_round_bg);
                chukuAfterLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                homeIcon.setImageResource(R.drawable.gray_round_bg);
                homeLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                abroadIcon.setImageResource(R.drawable.gray_round_bg);
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                if (status.equals("1") && currentLoginType == 2) {
                    chukuNewText.setVisibility(View.VISIBLE);
                }else if ((status.equals("2")||status.equals("3")) && currentLoginType == 1){
                    chukuNewText.setVisibility(View.VISIBLE);
                }else {
                    chukuNewText.setVisibility(View.GONE);
                }
                boxNewText.setVisibility(View.GONE);
                chukuAfterNewText.setVisibility(View.GONE);
                chukuContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.GONE);
                fengxiangOperationIcon.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                homeNewText.setVisibility(View.GONE);
                break;
            case "5":
                //封箱
                chukuParent.setClickable(true);
                if (currentLoginType == 1) {
                    boxParent.setClickable(true);
                    fengxiangOperationIcon.setVisibility(View.VISIBLE);
                } else {
                    boxParent.setClickable(false);
                    fengxiangOperationIcon.setVisibility(View.GONE);
                }
                chukuAfterParent.setClickable(false);
                homeParent.setClickable(false);
                abroadParent.setClickable(false);
                endParent.setClickable(false);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                chukuAfterIcon.setImageResource(R.drawable.gray_round_bg);
                chukuAfterLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                homeIcon.setImageResource(R.drawable.gray_round_bg);
                homeLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                abroadIcon.setImageResource(R.drawable.gray_round_bg);
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                if (status.equals("1")) {
                    boxNewText.setVisibility(View.VISIBLE);
                }
                chukuNewText.setVisibility(View.GONE);
                chukuAfterNewText.setVisibility(View.GONE);
                chukuContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                homeNewText.setVisibility(View.GONE);
                break;
            case "7":
                //出库
                chukuParent.setClickable(true);
                boxParent.setClickable(true);
                chukuAfterParent.setClickable(true);
                homeParent.setClickable(false);
                abroadParent.setClickable(false);
                endParent.setClickable(false);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));

                boxTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxLine.setBackgroundColor(mContext.getResources().getColor(R.color.green_color));

                chukuAfterTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuAfterLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                chukuAfterIcon.setImageResource(R.drawable.green_round_bg);

                homeIcon.setImageResource(R.drawable.gray_round_bg);
                homeLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                abroadIcon.setImageResource(R.drawable.gray_round_bg);
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                if (status.equals("1") && currentLoginType == 2) {
                    chukuAfterNewText.setVisibility(View.VISIBLE);
                }else if ((status.equals("2")||status.equals("3")) && currentLoginType == 1){
                    chukuAfterNewText.setVisibility(View.VISIBLE);
                }else {
                    chukuAfterNewText.setVisibility(View.GONE);
                }
                chukuNewText.setVisibility(View.GONE);
                boxNewText.setVisibility(View.GONE);
                homeNewText.setVisibility(View.GONE);
                chukuContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.VISIBLE);
                if (currentLoginType == 1) {
                    chukuOperationIcon.setVisibility(View.VISIBLE);
                } else {
                    chukuOperationIcon.setVisibility(View.GONE);
                }
                fengxiangOperationIcon.setVisibility(View.GONE);
                break;
            case "9":
                //国内货代
                chukuParent.setClickable(true);
                boxParent.setClickable(true);
                chukuAfterParent.setClickable(true);
                homeParent.setClickable(true);
                abroadParent.setClickable(false);
                endParent.setClickable(false);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuAfterTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuAfterLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                homeTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                homeLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                abroadIcon.setImageResource(R.drawable.gray_round_bg);
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                chukuContentText.setVisibility(View.VISIBLE);
                chukuAfterContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.VISIBLE);
                fengxiangOperationIcon.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                chukuAfterNewText.setVisibility(View.GONE);
                chukuNewText.setVisibility(View.GONE);
                boxNewText.setVisibility(View.GONE);
                homeNewText.setVisibility(View.VISIBLE);
                break;
            case "11":
                //国外货代
                chukuParent.setClickable(true);
                boxParent.setClickable(true);
                chukuAfterParent.setClickable(true);
                homeParent.setClickable(true);
                abroadParent.setClickable(true);
                endParent.setClickable(false);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuAfterTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                homeTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                abroadTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                abroadLine.setBackgroundColor(mContext.getResources().getColor(R.color.gray_color));
                endIcon.setImageResource(R.drawable.gray_round_bg);
                chukuContentText.setVisibility(View.VISIBLE);
                chukuAfterContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.VISIBLE);
                fengxiangOperationIcon.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                chukuAfterNewText.setVisibility(View.GONE);
                chukuNewText.setVisibility(View.GONE);
                boxNewText.setVisibility(View.GONE);
                homeNewText.setVisibility(View.GONE);
                break;
            default:
                //结束
                chukuParent.setClickable(true);
                boxParent.setClickable(true);
                chukuAfterParent.setClickable(true);
                homeParent.setClickable(true);
                abroadParent.setClickable(true);
                endParent.setClickable(true);
                chukuTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                boxTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuAfterTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                homeTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                abroadTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                endTitleText.setTextColor(mContext.getResources().getColor(R.color.main_text_color));
                chukuContentText.setVisibility(View.VISIBLE);
                chukuAfterContentText.setVisibility(View.VISIBLE);
                boxContentText.setVisibility(View.VISIBLE);
                fengxiangOperationIcon.setVisibility(View.GONE);
                chukuOperationIcon.setVisibility(View.GONE);
                chukuAfterNewText.setVisibility(View.GONE);
                chukuNewText.setVisibility(View.GONE);
                boxNewText.setVisibility(View.GONE);
                homeNewText.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void getProjectDetailFail() {

    }
}
